package com.theeviljames.coursework;

import java.io.File;
import java.util.Vector;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.theeviljames.coursework.ann.ANN;
import com.theeviljames.coursework.problems.FourBitParity;
import com.theeviljames.coursework.problems.ThreeBitParity;

public class ANNTester {
	
	private static final double[] learnRates = new double[]{
		0.05,0.1,0.2,0.3,0.4,0.5,
	};
	private static final double[] alphas = new double[]{
		0.5,0.7,0.9,
	};

	private static final int maxEpochs = 20000;
	private static final double maxError = 0.1;
	
	public static void main(String[] args) {
		try{
		for(double learnRate:learnRates){
			WritableWorkbook workBook = Workbook.createWorkbook(new File("ANNThreeBitParity"+ learnRate + ".xls"));
			int counter = 1;
			for(double alpha:alphas){
				String sheetname = "L" + learnRate + " A" + alpha;
				WritableSheet sheet = workBook.createSheet(sheetname, counter++);
				for(int i = 0; i < maxEpochs; i++){
					Label l = new Label(0,i+1,""+i);
					sheet.addCell(l);
				}
				for(int j = 0; j < 5; j++){
					ANN ann = new ANN(new int[]{3,3,2},learnRate,alpha,true);
					ann.run(new ThreeBitParity(), maxEpochs, maxError);
					Vector<Double> errors = ann.getErrorHistory();
					Label error = new Label(1+j,0,"Error on run " + (j+1));
					sheet.addCell(error);
					for(int i = 0; i < errors.size(); i++){
						Number n = new Number(1+j,i+1,errors.get(i));
						sheet.addCell(n);
					}	
				}
				
			}
			workBook.write();
			workBook.close();	
		}
		
		for(double learnRate:learnRates){
			WritableWorkbook workBook = Workbook.createWorkbook(new File("ANNFourBitParity" + learnRate+".xls"));
			int counter = 1;
			for(double alpha:alphas){
				String sheetname = "L" + learnRate + " A" + alpha;
				WritableSheet sheet = workBook.createSheet(sheetname, counter++);
				for(int i = 0; i < maxEpochs; i++){
					Label l = new Label(0,i+1,""+i);
					sheet.addCell(l);
				}
				for(int j = 0; j < 5; j++){
					ANN ann = new ANN(new int[]{4,4,2},learnRate,alpha,true);
					ann.run(new FourBitParity(), maxEpochs, maxError);
					Vector<Double> errors = ann.getErrorHistory();
					Label error = new Label(1+j,0,"Error on run " + (j+1));
					sheet.addCell(error);
					for(int i = 0; i < errors.size(); i++){
						Number n = new Number(1+j,i+1,errors.get(i));
						sheet.addCell(n);
					}	
				}
				
			}
			workBook.write();
			workBook.close();
		}
		
		}
		catch(Exception e){e.printStackTrace();}
	}
}
